package list

import C "gitee.com/ljfirst/algo-go-sdk/common/constant"

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/7/6 23:31
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type LastKTwoPoint struct {
}

func (m *LastKTwoPoint) FindMethod(root *Node, k int) *Node {
	if root == nil {
		return nil
	}
	quick, slow, cur := root, root, 1
	for quick != nil {
		if cur > k {
			slow = slow.Next
		}
		quick = quick.Next
		cur++
	}
	if cur < k {
		return nil
	}
	return slow
}
func (m *LastKTwoPoint) Delete(root *Node, k int) *Node {
	if root == nil || k <= 0 {
		return root
	}
	quick, slow, slowPre, cur := root, root, root, 1
	for quick != nil {
		if cur > k {
			slowPre = slow
			slow = slow.Next
		}
		quick = quick.Next
		cur++
	}
	if cur < k {
		return root
	}
	if slowPre != slow {
		slowPre.Next = slow.Next
	} else {
		root = root.Next
	}
	return root
}

func (m *LastKTwoPoint) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.List, C.TwoPoint},
		Desc: &C.Desc{
			Name:   "LastKTwoPoint",
			NameCn: "倒数第K个元素-双指针写法",
		},
	}
}
